<?php

class App_WebService_Request_SetApplicationData {


  /**
   *
   * @var App_WebService_Data_App
   */
  public $app;
  
  /**
   * 
   * @var App_WebService_Data_Informations[]
   */
  public $informations;
  
  /**
   *
   * @var App_WebService_Data_Links[]
   */
  public $links;

  public function process() {
		$users = array();
		$notFoundUsers = array();
		$defsCodes = array();
		foreach ($this->informations as $information) {
			if (!in_array($information->code, $defsCodes))
				$defsCodes[] = $information->code;
			if (!array_key_exists($information->userEmail, $users)) {
				$user = UsersQuery::create()->findOneByEmail($information->userEmail);
				if ($user instanceof Users) {
					$users[$information->userEmail] = $user;
				}
			}
		}
		foreach ($this->links as $link) {
			if (!in_array($link->code, $defsCodes))
				$defsCodes[] = $link->code;
			if (!array_key_exists($link->userEmail, $users)) {
				$user = UsersQuery::create()->findOneByEmail($link->userEmail);
				if ($user instanceof Users) {
					$users[$link->userEmail] = $user;
				}
			}
		}
		$applicationDataSet = ApplicationDataSetsQuery::create()
		->filterByStatus(ApplicationDataSets::STATUS_ACCEPTED)
		->orderByLastModified(Criteria::DESC)
		->findOneByApplicationId(Core_Core::getInstance()->getApplication()->getId());
		$applicationDataDefsCollection = ApplicationDataDefsQuery::create()
		->filterByCode($defsCodes)
		->filterByApplicationDataSets($applicationDataSet)
		->find();
		$applicationDataDefsArray = array();
		foreach ($applicationDataDefsCollection as $add) {
			$applicationDataDefsArray[$add->getCode()] = $add;
		}
		$response = new App_WebService_Response_SetApplicationDat();
		$con = Propel::getConnection();
		$con->beginTransaction();
		try {
	  	foreach ($this->informations as $information) {
	  		if (!array_key_exists($information->code, $applicationDataDefsArray) || $applicationDataDefsArray[$information->code]->getType() != ApplicationDataDefs::TYPE_INFO) {
	  			$response->addMissingCode($information->code);
	  			continue;
	  		}
	  		else {
	  			$applicationDataDef = $applicationDataDefsArray[$information->code];
	  		}
	  		
	  		if (!array_key_exists($information->userEmail, $users)) {
	  			$response->addMissingUser($information->userEmail);
	  			continue;
	  		}
	  		else {
	  			$user = $users[$information->userEmail];
	  		}
	  		file_put_contents(APPLICATION_PATH . '/logs/test.txt', $applicationDataDef->getId(), FILE_APPEND);
	  		$count = UserApplicationDataQuery::create()
	  		->filterByUserId($user->getId())
	  		->filterByApplicationDataDefId($applicationDataDef->getId())
	  		->update(array('Value' => $information->value), $con);
	  		if ($count == 0) {
		  		$userApplicationData = new UserApplicationData();
		  		$userApplicationData->setUsers($user);
		  		$userApplicationData->setApplicationDataDefs($applicationDataDef);
		  		$userApplicationData->setValue($information->value);
		  		$userApplicationData->save($con);
	  		}
	  	}
	  	foreach ($this->links as $link) {
	  		if (!array_key_exists($link->code, $applicationDataDefsArray) || $applicationDataDefsArray[$link->code]->getType() != ApplicationDataDefs::TYPE_LINK) {
	  			$response->addMissingCode($link->code);
	  			continue;
	  		}
	  		else {
	  			$applicationDataDef = $applicationDataDefsArray[$link->code];
	  		}
	  		 
	  		if (!array_key_exists($link->userEmail, $users)) {
	  			$response->addMissingUser($link->userEmail);
	  			continue;
	  		}
	  		else {
	  			$user = $users[$link->userEmail];
	  		}
				file_put_contents(APPLICATION_PATH . '/logs/test.txt', $applicationDataDef->getId(), FILE_APPEND);
	  		$count = UserApplicationDataQuery::create()
	  		->filterByUserId($user->getId())
	  		->filterByApplicationDataDefId($applicationDataDef->getId())
	  		->update(array('Value' => $link->value), $con);
	  		if ($count == 0) {
		  		$userApplicationData = new UserApplicationData();
		  		$userApplicationData->setUsers($user);
		  		$userApplicationData->setApplicationDataDefs($applicationDataDef);
		  		$userApplicationData->setValue($link->value);
		  		$userApplicationData->save($con);
	  		}
	  	}
	  	$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
		return $response;
  }

}